package com.example.xgakj.newterminal.ui.activities;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.xgakj.mylibrary.mvp.utils.DensityUtils;
import com.example.xgakj.mylibrary.mvp.utils.TimeUtil;
import com.example.xgakj.mylibrary.mvp.utils.ToastUtil;
import com.example.xgakj.newterminal.R;
import com.example.xgakj.newterminal.base.BaseActivity;
import com.example.xgakj.newterminal.base.Parameters;
import com.example.xgakj.newterminal.utils.FileUtil;
import com.example.xgakj.newterminal.views.GridDividerItemDecoration;
import com.example.xgakj.newterminal.views.ViewWrapper;
import com.qmuiteam.qmui.util.QMUIViewHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.socks.library.KLog;

import java.io.File;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dialogs.RxDialogScaleView;

public class ScreenShotsActivity extends BaseActivity {

    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<String> files;
    private QuickAdapter quickAdapter;
    private Button btnShow;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_end)
    TextView tvEnd;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    private ViewWrapper viewWrapper;
    private boolean isShowingSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_shots);
        ButterKnife.bind(this);
        initTopBar();
        initViews();
    }

    private void initViews() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(new GridDividerItemDecoration(10,getResources().getColor(R.color.text_color_white)));
        files = FileUtil.getPicFiles(Parameters.SCREEN_SHOTS);
        quickAdapter = new QuickAdapter();
        quickAdapter.openLoadAnimation();
        quickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                KLog.i(Parameters.TAG,(String) adapter.getData().get(position));
                RxDialogScaleView rxDialogScaleView = new RxDialogScaleView(mContex);
                rxDialogScaleView.setImagePath((String) adapter.getData().get(position));
                rxDialogScaleView.show();

            }
        });
        quickAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {
                new QMUIDialog.MessageDialogBuilder(ScreenShotsActivity.this)
                        .setTitle("永久删除图片")
                        .setMessage("确定要删除吗？")
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction(0, "删除", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                FileUtil.deleteFile(files.get(position));
                                files.remove(position);
                                quickAdapter.notifyItemRemoved(position);
                                Toast.makeText(ScreenShotsActivity.this, "删除成功"+position, Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        })
                        .show();

                return true;
            }
        });
        recyclerView.setAdapter(quickAdapter);
    }

    private void initTopBar() {
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

                overridePendingTransition(R.anim.slide_still, R.anim.slide_out_right);
            }
        });
        topbar.setTitle("截图");
        btnShow = topbar.addRightTextButton("搜索", QMUIViewHelper.generateViewId());
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowingSearch){
                    btnShow.setText("搜索");
                    dismissSearchView();
                }else {
                    btnShow.setText("收起");
                    showSearchView();
                }
            }
        });
    }
    private void showSearchView() {
        isShowingSearch=true;
        viewWrapper = new ViewWrapper(llSearch);
        ObjectAnimator.ofInt(viewWrapper, "height", DensityUtils.dip2px(mContex, 56)).setDuration(300).start();

    }
    private void dismissSearchView() {
        isShowingSearch=false;
        if (viewWrapper==null){
            return;
        }
        ObjectAnimator.ofInt(viewWrapper, "height", DensityUtils.dip2px(mContex, 0)).setDuration(300).start();

    }
    public class QuickAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        public QuickAdapter() {
            super(R.layout.item_screen_shot, files);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final String item) {
            File file=new File(item);
            Glide.with(ScreenShotsActivity.this).load(file).into((ImageView) helper.getView(R.id.iv_screenshot));


        }
    }
    @OnClick({R.id.tv_start, R.id.tv_end, R.id.iv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_start:
                TimePickerView startTimeView = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        tvStart.setText(TimeUtil.Date2String(date));
                    }
                }).setType(new boolean[]{true, true, true, false, false, false})
                        .build();
                startTimeView.show();
                break;
            case R.id.tv_end:
                TimePickerView endTimeView = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        tvEnd.setText(TimeUtil.Date2String(date));
                    }
                }).setType(new boolean[]{true, true, true, false, false, false})
                        .build();
                endTimeView.show();
                break;
            case R.id.iv_search:
                String start_value = tvStart.getText().toString().trim();
                String end_value = tvEnd.getText().toString().trim();
                long start= TimeUtil.String2long(start_value);
                long end= TimeUtil.String2long(end_value);
                if (start==0||end==0||start>end){
                    ToastUtil.showShort(mContex,"请选择正确的时间段查询");
                    return;
                }
                List<String> list = FileUtil.selectFilesByTime(Parameters.SCREEN_SHOTS, ".png", "", start, end,"yyyy-MM-dd HH:mm:ss");
                if (list!=null){
                    KLog.e(list.size());
                    files.clear();
                    files.addAll(list);
                    quickAdapter.notifyDataSetChanged();
                    btnShow.setText("搜索");
                    dismissSearchView();
                }
                break;
            default:
        }
    }
}
